<template>
    <el-dialog @close="cancelAddTechnique" v-model="dialogVisible" width="30%" title="新增工艺路线信息" destroy-on-close center>
        <template #default>
            <el-form :model="technique" label-width="100px" label-position="left">
                <el-form-item label="工艺路线编码">
                    <el-input v-model="technique.techniqueCode" aria-placeholder="工艺路线编码" />
                </el-form-item>
                <el-form-item label="工艺路线名称">
                    <el-input v-model="technique.techniqueName" aria-placeholder="工艺路线名称" />
                </el-form-item>
                <el-form-item label="是否启用">
                    <el-radio-group v-model="technique.techniqueStatus">
                        <el-radio :label="1">是</el-radio>
                        <el-radio :label="2">否</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="工艺路线说明">
                    <el-input type="textarea" v-model="technique.description" aria-placeholder="工艺路线说明" />
                </el-form-item>
                <el-form-item label="备注">
                    <el-input type="textarea" v-model="technique.notes" aria-placeholder="备注" />
                </el-form-item>
            </el-form>
        </template>
        <!-- footer插槽中放两个按钮 -->
        <template #footer>
            <el-row>
                <el-col :span="12">
                    <el-button type="primary" @click="addTechnique">确定</el-button>
                </el-col>
                <el-col :span="12">
                    <el-button type="primary" @click="cancelAddTechnique">取消</el-button>
                </el-col>
            </el-row>
        </template>
    </el-dialog>
</template>
<script>
import { watch } from "vue";
// 引入我们申明的technique的api的js
import techniqueRequest from '../../../api/technique.js';
export default {
    props: ["visible"],
    emits: ["optionCompleted"], // 对外暴露一个事件，用来传递添加工艺的操作结果
    data() {
        return {
            technique: {},// 绑定新增的工艺信息
            dialogVisible: false,// 添加工艺窗口的显示与隐藏
        }
    },
    methods: {
        /**
         * 取消添加工艺的事件
         */
        cancelAddTechnique(){
            this.technique = {};
            // 通知父组件  操作完成，让父组件关闭窗体
            this.$emit("optionCompleted");
        },
        /**
         * 添加工艺信息
         */
        addTechnique(){
            techniqueRequest.addTechnique(this.technique,res=>{
                if (res.code == '200'){
                    this.$message({
                        type: 'success',
                        message: '添加工艺路线成功!'
                    });
                    // 通知父组件  操作完成，让父组件关闭窗体
                    this.$emit("optionCompleted");
                }else if(res.code == '300'){
                    this.$message({
                        type: 'warning',
                        message: '工艺路线' + (res.msg.indexOf("techniqueName") > 0 ?"名称":"类型") + "重复"
                    });
                }else{
                    this.$message({
                        type: 'error',
                        message: '添加工艺路线失败!'
                    });
                }
            });
        },
    }, mounted(props) {
        // 侦听props   一旦发生变化，就会出发下面的函数
        watch(() => this.visible, (newVal, oldVal) => {
            // 将最新的值设置给弹窗的dialogVisible属性
            this.dialogVisible = newVal;
        });
    }
}
</script>